package com.yan.activity;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yan.R;

public class ArticlesListAdapter extends ArrayAdapter<Article> implements OnItemClickListener
{
	private static final String TAG = "articleS_LIST_ADAPTER";

	protected LayoutInflater mInflater;
	protected List<Article> mList;
	protected int mnViewResId;
	protected Article mCurrentarticle;
	protected DataManager mDataManager;
	protected Context mContext;
	
	public ArticlesListAdapter(Context context, List<Article> list, int resId) 
	{
		super(context, resId, list);
		mInflater = LayoutInflater.from(context);
		mList = list;
		mnViewResId = resId;
		mDataManager = DataManager.getInstance();
		mContext = context;
	}

	@Override
	public int getCount() 
	{
		int size = 0;
		if(mList != null)
		{
			size = mList.size();
		}
		return size;
	}
	
	@Override
	public long getItemId(int position) 
	{
		return position;
	}


	static class articlesViewHolder
	{
		RelativeLayout articleTrailerClickZone;
		ImageView articleImage;
		TextView articleTitle;
		TextView articleLength;
		ImageView articleTrailerPlay;
	     // Reference the Gallery view
        BetterGallery g;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		
		articlesViewHolder holder = null;
		if(convertView == null)
		{
			convertView = mInflater.inflate(mnViewResId, null);
			holder = new articlesViewHolder();			
			
			//On associe les donnes
			holder.articleImage = (ImageView) convertView.findViewById (R.id.article_image);
			holder.articleTitle = (TextView) convertView.findViewById (R.id.article_title);
			holder.g= (BetterGallery) convertView.findViewById(R.id.gallery);
	        
			
			convertView.setTag(holder);
		}
		else 
		{
			holder = (articlesViewHolder) convertView.getTag();
		}

		//On associe les donnes avec le holder correspondant
		mCurrentarticle = getItem(position);
		
		//holder.articleTrailerClickZone.setTag(mCurrentarticle.getTrailer());
		
		holder.articleImage.setTag(mCurrentarticle.getLink());
		//holder.articleImage.setImageBitmap(mDataManager.getPosterImage());
		mDataManager.setBitmapToImageView(mCurrentarticle.getThumbnailURL(), holder.articleImage, mCurrentarticle.getLink());
		
		holder.articleTitle.setText(mCurrentarticle.getTitle());
		//holder.articleTitle.setCompoundDrawablesWithIntrinsicBounds(0, 0, mDataManager.getResIdForCertificate(mCurrentarticle.getCertificate()), 0);
		holder.articleTitle.setCompoundDrawablePadding(3); 
		
		holder.g.setTag(mCurrentarticle.getLink());
		mDataManager.setImageListToGallery(mCurrentarticle.getLink(), holder.g, mCurrentarticle, mContext);
	
			//starring = new SpannableString(mCurrentarticle.getPubDate().toGMTString());
			//starring.setSpan(new StyleSpan(Typeface.BOLD), 0, mCurrentarticle.getPubDate().toGMTString().length(), 0);
		
		
		//holder.articleStarring.setText(starring);
		
		// Set the adapter to our custom adapter (below)
	
		
		//On retourne la vue
		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
	{
		Article article = getItem(position);
		if(article != null);
			
			
	}

	

    
	
	
}
